package com.sy.controller.permissions;

import com.sy.bean.permissions.Menu;
import com.sy.bean.permissions.SaveMenu;
import com.sy.bean.permissions.ShowLinkMenu;
import com.sy.bean.permissions.StatusMenu;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.service.permissions.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "菜单权限管理")
@RequestMapping("/api/permissions")
public class MenuController extends BaseController {
    @Autowired
    MenuService menuService;
    @GetMapping("/menu/tree")
    @ApiOperation("级联菜单列表")
    public ResultBean queryTree() {
        List<Map> list = menuService.queryList(null);
        return success("级联菜单列表",list);
    }
    @PostMapping("/menu/insert")
    @ApiOperation("新增菜单")
    public ResultBean save(@RequestBody SaveMenu menu) {
        int rs = menuService.save(menu);
        return gotoResult(rs,"新增");
    }
    @GetMapping("/delete")
    @ApiOperation("删除菜单")
    public ResultBean delete(int id){
        int rs = menuService.delete(id);
        return gotoResult(rs,"删除");
    }
    @GetMapping("/on/tree")
    @ApiOperation("上级菜单树")
    public ResultBean tree(){
        List<Map> list = menuService.tree();
        return success("上级菜单树",list);
    }
    @PutMapping("/edit/status")
    @ApiOperation("修改菜单状态")
    public ResultBean editStatus(@RequestBody StatusMenu menu){
        int rs = menuService.editStatus(menu);
        return gotoResult(rs,"修改");
    }
    @PutMapping("/edit/showLink")
    @ApiOperation("修改菜单是否显示")
    public ResultBean editShowLink(@RequestBody ShowLinkMenu menu){
        int rs = menuService.editShowLink(menu);
        return gotoResult(rs,"修改");
    }
    @PutMapping("/edit")
    @ApiOperation("修改菜单")
    public ResultBean update(@RequestBody Menu menu){
        int rs = menuService.update(menu);
        return gotoResult(rs,"修改");
    }

    @GetMapping("/queryMenuByUserId")
    @ApiOperation("根据用户id查询用户权限")
    public ResultBean queryMenuByUserId(@RequestParam(name = "id",required = true) int id){
        Map map = menuService.queryMenuByUserId(id);
        return success("用户权限",map);
    }
}
